package cn.wxManage.goodsManage.service.impl;

import cn.common.common.model.BaseStatus;
import cn.common.common.model.ServerResponse;
import cn.wxManage.common.dao.ImageInfoMapper;
import cn.wxManage.goodsManage.dao.GoodsCartMapper;
import cn.wxManage.goodsManage.dao.GoodsOrderItemMapper;
import cn.wxManage.goodsManage.dao.GoodsOrderMapper;
import cn.wxManage.goodsManage.model.GoodsCart;
import cn.wxManage.goodsManage.model.GoodsOrder;
import cn.wxManage.goodsManage.model.GoodsOrderItem;
import cn.wxManage.goodsManage.service.IGoodsOrderService;
import cn.wxManage.user.model.UserInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Zhang on 2019/1/24.
 */
@Service
public class GoodsOrderService implements IGoodsOrderService{
    @Autowired
    GoodsOrderMapper goodsOrderMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    GoodsOrderItemMapper goodsOrderItemMapper;

    @Autowired
    GoodsCartMapper goodsCartMapper;

    @Autowired
    ImageInfoMapper imageInfoMapper;

    @Override
    public ServerResponse<?> addOrderInGoodsView(GoodsOrder goodsOrder) {
        UserInfo userInfo = getUserInfo();
        List<GoodsOrderItem> goodsOrderItemList = goodsOrder.getOrderItemList();
        if (goodsOrderItemList == null || goodsOrderItemList.size()==0){
            return ServerResponse.createByErrorMessage("请重新选择心仪的商品");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH24mmss");
        goodsOrder.setOrderNo(simpleDateFormat.format(new Date()));
        goodsOrder.setUserId(userInfo.getUserid().toString());
        goodsOrder.setStatus(BaseStatus.orderPendingEnter);
        goodsOrder.setCreateTime(new Date());
        goodsOrderMapper.insertSelective(goodsOrder);
        GoodsOrderItem item = goodsOrderItemList.get(0);
        item.setOrderNo(goodsOrder.getOrderNo());
        item.setUserId(userInfo.getUserid().toString());
        goodsOrderItemMapper.insertSelective(item);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> addOrderInCartView(List<Integer> ids,GoodsOrder goodsOrder) {
        UserInfo userInfo = getUserInfo();
        List<GoodsOrderItem> goodsOrderItemList = goodsOrder.getOrderItemList();
        if (goodsOrderItemList == null || goodsOrderItemList.size()==0 ||
                ids == null || ids.size()==0){
            return ServerResponse.createByErrorMessage("请重新选择心仪的商品");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH24mmss");
        goodsOrder.setOrderNo(simpleDateFormat.format(new Date()));
        goodsOrder.setUserId(userInfo.getUserid().toString());
        goodsOrder.setStatus("pendingEnter");
        goodsOrder.setCreateTime(new Date());
        goodsOrderMapper.insertSelective(goodsOrder);
        for (GoodsOrderItem goodsOrderItem:goodsOrderItemList){
            goodsOrderItem.setOrderNo(goodsOrder.getOrderNo());
            goodsOrderItem.setUserId(userInfo.getUserid().toString());
            goodsOrderItemMapper.insertSelective(goodsOrderItem);
        }
        goodsCartMapper.updateStatusByIds(ids,BaseStatus.cartHadEntry);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> getOrderByStatus(String status) {
        List<GoodsOrder> goodsOrderList = goodsOrderMapper.getOrderListByStatus(status);
        for (GoodsOrder goodsOrder: goodsOrderList){
            List<GoodsOrderItem> orderItemList = goodsOrderItemMapper.getItemListByOrderNo(goodsOrder.getOrderNo());
            for (GoodsOrderItem item:orderItemList){
                item.setMainImg(imageInfoMapper.getGoodsMain(item.getGoodsId()).get(0));
            }
            goodsOrder.setOrderItemList(orderItemList);
        }
        return ServerResponse.createBySuccess(goodsOrderList);
    }

    @Override
    public ServerResponse<?> getOrderInfo(Integer id) {
        GoodsOrder goodsOrder = goodsOrderMapper.getOrderInfo(id);
        List<GoodsOrderItem> orderItemList = goodsOrderItemMapper.getItemListByOrderNo(goodsOrder.getOrderNo());
        for (GoodsOrderItem item:orderItemList){
            item.setMainImg(imageInfoMapper.getGoodsMain(item.getGoodsId()).get(0));
        }
        goodsOrder.setOrderItemList(orderItemList);
        return ServerResponse.createBySuccess(goodsOrder);
    }

    private UserInfo getUserInfo(){
        return (UserInfo) request.getAttribute("userInfo");
    }
}
